package com.new_wear.project.system.info.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.new_wear.common.utils.file.FileUploadUtils;
import com.new_wear.framework.config.NewEraConfig;
import com.new_wear.project.system.pos.domain.CstAdPos;
import com.new_wear.project.system.pos.service.ICstAdPosService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.info.domain.CstAdInfo;
import com.new_wear.project.system.info.service.ICstAdInfoService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 广告 - 广告Controller
 * 
 * @author ruoyi
 * @date 2019-12-19
 */
@Controller
@RequestMapping("/system/info")
public class CstAdInfoController extends BaseController
{
    private String prefix = "system/info";

    @Autowired
    private ICstAdInfoService cstAdInfoService;

    @Autowired
    private ICstAdPosService cstAdPosService;

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }

    /**
     * 查询广告 - 广告列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CstAdInfo cstAdInfo)
    {
        startPage();
        List<CstAdInfo> list = cstAdInfoService.selectCstAdInfoList(cstAdInfo);
        for (CstAdInfo cstAdInfoList:list) {
            Integer status = cstAdInfoList.getStatus();
            if (status == 0){
                cstAdInfoList.setStringStutas("下架");
            }else if (status == 1){
                cstAdInfoList.setStringStutas("上架");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出广告 - 广告列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "广告 - 广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstAdInfo cstAdInfo)
    {
        List<CstAdInfo> list = cstAdInfoService.selectCstAdInfoList(cstAdInfo);
        ExcelUtil<CstAdInfo> util = new ExcelUtil<CstAdInfo>(CstAdInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 新增广告 - 广告
     */
    @GetMapping("/add")
    public String add() {

        return prefix + "/add";
    }



    @GetMapping("/addadpos")
    public String addadpos() {

        return prefix + "/position";
    }
    /*广告位置列表*/
    @RequestMapping("/adpos")
    @ResponseBody
    public TableDataInfo adpos(CstAdPos cstAdPos){
        startPage();
        List<CstAdPos> list = cstAdPosService.selectCstAdPosList(cstAdPos);

        return getDataTable(list);
    }

    @RequestMapping("/adposId")
    @ResponseBody
    public CstAdPos cstAdPos(Integer id){
        startPage();
        CstAdPos cstAdPos = cstAdPosService.selectCstAdPosById(id);

        return cstAdPos;
    }
    /**
     * 新增保存广告 - 广告
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "广告 - 广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, CstAdInfo cstAdInfo,@RequestParam("hideinfo") Integer hideinfo) throws Exception {
        String filePath = NewEraConfig.getUploadPath();
        System.out.println(filePath);
        String fileName  = FileUploadUtils.upload(filePath, file);

        CstAdInfo cstAdInfoc = new  CstAdInfo();


            //添加
            cstAdInfoc.setCreatedAt(new Date());//创建时间

        cstAdInfoc.setAdId(cstAdInfo.getAdId());//客户端广告记录编号
        cstAdInfoc.setAdPos(hideinfo);//广告位position
        cstAdInfoc.setAreaId(cstAdInfo.getAreaId());//区域ID
        cstAdInfoc.setTitle(cstAdInfo.getTitle());//广告名称
        cstAdInfoc.setDescription(cstAdInfo.getDescription());//摘要
        cstAdInfoc.setIconUrl(fileName);//图片地址
        cstAdInfoc.setIsExt(cstAdInfo.getIsExt());////是否是外链，1是。0不是
        cstAdInfoc.setShowTitle(cstAdInfo.getShowTitle());//是否显示头部（0不显示；1显示）
        cstAdInfoc.setStartedAt(cstAdInfo.getStartedAt());//有效期 开始时间
        cstAdInfoc.setExpiredIn(cstAdInfo.getExpiredIn());//有效期 结束时间
        cstAdInfoc.setStatus(0);//状态 1上架; 0下架
        cstAdInfoc.setUrl(cstAdInfo.getUrl());//链接
        cstAdInfoc.setSeq(cstAdInfo.getSeq());//排序
        return toAjax(cstAdInfoService.insertCstAdInfo(cstAdInfoc));
    }

    /**
     * 修改广告 - 广告
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstAdInfo cstAdInfo = cstAdInfoService.selectCstAdInfoById(id);
        mmap.put("cstAdInfo", cstAdInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存广告 - 广告
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "广告 - 广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam("file") MultipartFile file,CstAdInfo cstAdInfo)throws Exception{
       if (file!=null){
           String filePath = NewEraConfig.getUploadPath();
           System.out.println(filePath);
           String fileName  = FileUploadUtils.upload(filePath, file);
           cstAdInfo.setIconUrl(fileName);
           cstAdInfo.setModifiedAt(new Date());
       }
        return toAjax(cstAdInfoService.updateCstAdInfo(cstAdInfo));
    }

    /**
     * 删除广告 - 广告
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "广告 - 广告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstAdInfoService.deleteCstAdInfoByIds(ids));
    }


    /*发布   取消发布接口*/
    @RequiresPermissions("system:info:release")
    @Log(title = "广告 - 广告", businessType = BusinessType.UPDATE)
    @PostMapping( "/release")
    @ResponseBody
    public AjaxResult release(Integer id) {
        CstAdInfo cstAdInfo = cstAdInfoService.selectCstAdInfoById(id);
        if (cstAdInfo.getStatus() ==1){
        cstAdInfo.setStatus(0);
        }else if (cstAdInfo.getStatus() ==0){
            cstAdInfo.setStatus(1);
        }
        return toAjax(cstAdInfoService.updateCstAdInfo(cstAdInfo));
    }

    }
